package v1

import (
	"ginblog/middleware"
	"ginblog/model"
	"ginblog/utils/errmsg"
	"github.com/gin-gonic/gin"
	"net/http"
)

// Login 用户登录
// @Summary 用户登录
// @Description 用户使用用户名和密码登录
// @Tags Auth
// @Accept json
// @Produce json
//
// @Router /api/v1/login [post]
func Login(c *gin.Context) {
	var data model.User
	var token string
	c.ShouldBindJSON(&data)
	code = model.CheckLogin(data.Username, data.Password)
	if code == errmsg.SUCCSE {
		token, _ = middleware.SetToken(data.Username)
	}
	c.JSON(http.StatusOK, gin.H{
		"status":  code,
		"token":   token,
		"message": errmsg.GetErrorMsg(code),
	})

}

// LoginFront 前台用户登录
// @Summary 前台用户登录
// @Description 前台用户使用用户名和密码登录
// @Tags Auth
// @Accept json
// @Produce json
// @Router /api/v1/login/front [post]
func LoginFront(c *gin.Context) {
	var formData model.User
	_ = c.ShouldBindJSON(&formData)
	var token string
	var code int

	formData, code = model.CheckLoginFront(formData.Username, formData.Password)

	c.JSON(http.StatusOK, gin.H{
		"status":  code,
		"data":    formData.Username,
		"id":      formData.ID,
		"message": errmsg.GetErrorMsg(code),
		"token":   token,
	})
}
